package com.jiu.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class XmlUtils {
	public static String parseFishingBagTable(String xml) {
		return xml;
	}

	public static String parseFishingBankTable(String xml) {
		return xml;
	}

	/**
	 * @description 将xml字符串转换成map
	 * @param xml
	 * @return Map
	 */
	@SuppressWarnings("unchecked")
	public static List<Map<String, String>> parseFishBagAndBankTableXml(String xml) {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		Document doc = null;
		try {
			// 将字符串转为XML
			doc = DocumentHelper.parseText(xml);
			// 获取根节点
			Element rootElt = doc.getRootElement();
			// 拿到DocumentElement节点
			Element e1 = rootElt.element("DocumentElement");
			// 拿到DocumentElement节点下所有fishbag节点
			List<Element> eleList = e1.elements();
			for (Element ele : eleList) {
				Map<String, String> eleMap = new HashMap<String, String>();
				Iterator<Element> iterss = ele.elementIterator();
				while (iterss.hasNext()) {
					Element e = iterss.next();
					eleMap.put(e.getName(), e.getTextTrim());
				}
				list.add(eleMap);
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public static void main(String[] args) throws IOException {/*
																 * StringBuffer
																 * sb = new
																 * StringBuffer
																 * (); String
																 * line = null;
																 * BufferedReader
																 * br = new
																 * BufferedReader
																 * (new
																 * FileReader(
																 * "src/com/jiu/util/bag.xml"
																 * ));
																 * while((line
																 * =br
																 * .readLine()
																 * )!=null)
																 * sb.append
																 * (line);
																 * 
																 * System.out.
																 * println
																 * (readStringXmlOut
																 * (
																 * sb.toString()
																 * ));
																 */
	}
}
